#include <stdlib.h>
#include <stdio.h>
#include <nds.h>
#include <xlibrary.h>
#include <string.h>

// global headers
#include "common.h"

// local headers
#include "video_window.h"

#include "zone.h"

/*
 * by default a zone is :
 *	not clickable
 *	auto sized
 *	not scrollable
 */

sZone *z_copy(sZone *zone) {
	if(!zone) return NULL;

	zone=(sZone *)xmemdup((void *)zone, sizeof(sZone));

	zone->text=xstrdup(zone->text);
	zone->pic=(sPicture *)xmemdup((void *)zone->pic, sizeof(sPicture));	// don't copy the content of the structure

	return zone;
}

int z_makeButton(sZone *zone, char *text, sPicture *pic) {
	if(!zone) return 1;

	bzero((void *)zone, sizeof(sZone));

	zone->flags=fZ_CLICKABLE | fZ_FIXEDSIZE;

	zone->text=text;
	zone->pic=pic;

	zone->width=/*t_getWidth(text)*/text?(strlen(text)*8):0 + pic?pic->width:0;
	zone->height=MAX(/*t_getHeight(text)*/text?8:0, pic?pic->height:0);

	return 0;
}

int z_render(sZone *zone, s16 awidth, s16 aheight) {
	if(!zone) return 1;

	u16 w=zone->width+awidth;
	u16 h=zone->height+aheight;
	u32 f=zone->flags;

	video_putSquare(0, 0, w, h, RGB15(31, 0, 0)|BIT(15));	// reset to red

	if(f & fZ_CLICKABLE)
		video_putSquare(1, 1, w-2, h-2, RGB15(0, 0, 31)|BIT(15));	// blue square
	if(f & fZ_FIXEDWIDTH)
		video_putLine(0, 0, w-1, h-1, RGB15(0, 0, 0)|BIT(15));	// black line
	if(f & fZ_FIXEDHEIGHT)
		video_putLine(0, h-1, w-1, 0, RGB15(0, 0, 0)|BIT(15));	// black line
	if(f & fZ_SCROLLABLE)
		video_putLine(w-1, 0, w-1, h-1, RGB15(20, 20, 20)|BIT(15));	// grey line

	return 0;
}

